package sy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.jackson.IotPatrolURIProjectMappingMixin;
import sy.model.IotPatrolURI;
import sy.model.IotPatrolURIProjectMapping;
import sy.model.SysProject;
import sy.service.IotPatrolURIServiceI;
import sy.service.IotPatrolURIProjectMappingServiceI;
import sy.service.SysProjectServiceI;
import sy.util.MessageObject;


@Controller
@RequestMapping("iotPatrolURIProjectMappingController")
public class IotPatrolURIProjectMappingController {
	@Autowired
	IotPatrolURIProjectMappingServiceI iotPatrolURIProjectMappingService;
	@Autowired
	IotPatrolURIServiceI iotPatrolURIService;
	@Autowired
	SysProjectServiceI sysProjectService;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	/************************** view controller ***************************/

	/**
	 * 
		 * @Title      : objectView
		 * @Description: 首页视图
		 * @param      : model
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 
		 * @throws
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
	
		return "iotInterface/patrolURI/patrolURIPrejectMapping";
	}
	
	/**
	 * 
		 * @Title      : newObjectInfo
		 * @Description:  TODO
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:05:21
		 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		IotPatrolURIProjectMapping patrolURIProjectMapping = new IotPatrolURIProjectMapping();
		
		List<IotPatrolURI> iotPatrolURIList = iotPatrolURIService.list();
		List<SysProject> sysProjectList = sysProjectService.list();

		model.addAttribute("patrolURIProjectMapping",patrolURIProjectMapping);
		model.addAttribute("iotPatrolURIList",iotPatrolURIList);
		model.addAttribute("sysProjectList",sysProjectList);
		model.addAttribute("typeCode",1);
		
		return "iotInterface/patrolURI/patrolURIPrejectMappingFrom";
	}

	/**
	 * 
		 * @Title      : editObjectInfo
		 * @Description:  TODO
		 * @param      :@param id
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:06:20
		 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam int id,Model model){
			
		IotPatrolURIProjectMapping patrolURIProjectMapping = iotPatrolURIProjectMappingService.getIotPatrolURIProjectMappingById(id);
		
		List<IotPatrolURI> iotPatrolURIList = iotPatrolURIService.list();
		List<SysProject> sysProjectList = sysProjectService.list();

		model.addAttribute("patrolURIProjectMapping",patrolURIProjectMapping);
		model.addAttribute("iotPatrolURIList",iotPatrolURIList);
		model.addAttribute("sysProjectList",sysProjectList);
		model.addAttribute("typeCode",2); 
		
		return "iotInterface/patrolURI/patrolURIPrejectMappingFrom";
	}
	
	/************************** data controller ***************************/
	/**
	 * 
		 * @Title      : getAllData
		 * @Description:  TODO
		 * @param      :@param response
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:41:07
		 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {
		objectMapper.addMixInAnnotations(IotPatrolURIProjectMapping.class, IotPatrolURIProjectMappingMixin.BasicInfo.class);
		List<IotPatrolURIProjectMapping> iotPatrolURIProjectMappingList = iotPatrolURIProjectMappingService.list();
		try {
			return objectMapper.writeValueAsString(iotPatrolURIProjectMappingList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 
	 * @Title		:getIotPatrolURIProjectMappingByKey
	 * @Description	:TODO
	 * @param		:@param key
	 * @param		:@return
	 * @return		:List<IotPatrolURIProjectMapping>
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:38:46
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotPatrolURIProjectMappingByKey", method = RequestMethod.POST)
	private List<IotPatrolURIProjectMapping> getIotPatrolURIProjectMappingByKey(@RequestParam String key) {
		List<IotPatrolURIProjectMapping> list = iotPatrolURIProjectMappingService.listIotPatrolURIProjectMappingByKey(key);
		return list;
	}
	
	/**
	 * 
	 * @Title		:getIotPatrolURIProjectMappingById
	 * @Description	:TODO
	 * @param		:@param id
	 * @param		:@return
	 * @return		:IotPatrolURIProjectMapping
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:39:30
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotPatrolURIProjectMappingById", method = RequestMethod.POST)
	private IotPatrolURIProjectMapping getIotPatrolURIProjectMappingById(@RequestParam Integer id) {
		IotPatrolURIProjectMapping obj = iotPatrolURIProjectMappingService.getIotPatrolURIProjectMappingById(id);
		return obj;
	}
	
	/**
	 * 
	 * @Title		:newObjectInfo
	 * @Description	:TODO
	 * @param		:@param iotPatrolURIProjectMapping
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午6:21:05
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo(@RequestBody IotPatrolURIProjectMapping iotPatrolURIProjectMapping) {
		MessageObject messageObject = new MessageObject();
		try {
			iotPatrolURIProjectMappingService.save(iotPatrolURIProjectMapping);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:editObjectInfo
	 * @Description	:TODO
	 * @param		:@param iotPatrolURIProjectMapping
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:44:58
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody IotPatrolURIProjectMapping iotPatrolURIProjectMapping) {
		MessageObject messageObject = new MessageObject();
		try {
			iotPatrolURIProjectMappingService.update(iotPatrolURIProjectMapping);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:delObjectInfo
	 * @Description	:TODO
	 * @param		:@param ids
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:44:52
	 * @throws
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String ids) {
		MessageObject messageObject = new MessageObject();
		String[] strs = ids.split(",");
		List<Integer> idArray = new ArrayList<Integer>();
		for(String str : strs){
			idArray.add(Integer.parseInt(str));
		}
		try {
			iotPatrolURIProjectMappingService.delete(idArray);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
}
